package com.bingxue.edu.management.system.controller;

import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.common.model.Pagination;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.framework.common.model.SortQuery;
import com.bingxue.edu.framework.mybatis.query.QueryBuilder;
import com.bingxue.edu.framework.security.ApiResource;
import com.bingxue.edu.management.system.model.query.StorageQuery;
import com.bingxue.edu.management.system.model.req.StorageReq;
import com.bingxue.edu.management.system.model.resp.StorageDetailResp;
import com.bingxue.edu.management.system.model.resp.StorageResp;
import com.bingxue.edu.management.system.service.StorageService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 存储配置接口
 *
 * @author panbo
 * @since 2024/07/22 23:33
 */
@Tag(name = "存储配置接口")
@RestController
@RequestMapping("/system/storage")
@RequiredArgsConstructor
public class StorageController {

    private final StorageService storageService;

    @Operation(summary = "查询列表")
    @ApiResource("system:storage:manage")
    @GetMapping("/list")
    public Response<List<StorageResp>> list(StorageQuery query, SortQuery sortQuery) {
        QueryWrapper queryWrapper = QueryBuilder.build(query, sortQuery);
        List<StorageResp> list = storageService.listAs(queryWrapper, StorageResp.class);
        return Response.success(list);
    }

    @Operation(summary = "分页查询列表")
    @ApiResource("system:storage:manage")
    @GetMapping("/page")
    public Response<Pagination<StorageResp>> page(StorageQuery query, @Validated PageQuery pageQuery) {
        QueryWrapper queryWrapper = QueryBuilder.build(query, pageQuery);
        Page<StorageResp> page = storageService.pageAs(pageQuery.toPage(), queryWrapper, StorageResp.class);
        return Response.success(Pagination.of(page));
    }

    @Operation(summary = "查询详情")
    @ApiResource("system:storage:manage")
    @GetMapping("/detail/{id}")
    public Response<StorageDetailResp> detail(@PathVariable String id) {
        StorageDetailResp detail = storageService.getMapper().selectOneWithRelationsByIdAs(id, StorageDetailResp.class);
        return Response.success(detail);
    }

    @Operation(summary="创建数据")
    @ApiResource("system:storage:manage")
    @PostMapping("/create")
    public Response<Void> create(@RequestBody @Validated StorageReq req) {
        storageService.create(req);
        return Response.success();
    }

    @Operation(summary="更新数据")
    @ApiResource("system:storage:manage")
    @PostMapping("/update/{id}")
    public Response<Void> update(@RequestBody @Validated StorageReq req, @PathVariable String id) {
        storageService.update(req, id);
        return Response.success();
    }

    @Operation(summary="删除数据")
    @ApiResource("system:storage:manage")
    @PostMapping("/delete/{ids}")
    public Response<Void> delete(@PathVariable List<String> ids) {
        storageService.delete(ids);
        return Response.success();
    }

}
